package adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.qianfeng.lingshijia.DetailsActivity;

import java.util.ArrayList;
import java.util.List;

import custom.GuessView;
import entity.Details;

public class DetailsAdapater extends BaseAdapter {
    private List<Details.DataEntity.GuessLikeListEntity> data;
    private Context context;

    public DetailsAdapater(Context context){
        this.context = context;
        data = new ArrayList<>();
    }

    public void setData(List<Details.DataEntity.GuessLikeListEntity> data)
    {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private  String imgUrl,title,priceStr,originalPriceStr;
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if(convertView == null){
            convertView = new GuessView(context);
            vh = new ViewHolder();
            vh.guessView = (GuessView) convertView;
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }
        imgUrl = data.get(position).getImgUrl();
        title = data.get(position).getTitle();
        priceStr = "¥"+data.get(position).getPriceStr();
        originalPriceStr = "¥"+data.get(position).getOriginalPriceStr();
        vh.guessView.setData(imgUrl,title,priceStr,originalPriceStr);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, DetailsActivity.class);
                intent.putExtra("productId", data.get(position).getId());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder
    {
        GuessView guessView;
    }
}
